var moveable=false;
var x0=0,y0=0,x1=0,y1=0,xx=0,yy=0;
var target;
var normal;
var container = [];
var div;
var win;
var topDiv;
var winInfo;
var lastEx=0;
var lastEy=0;

function startMove(obj){
	win = obj.parentNode;//.parentNode
	winInfo = getInfo(win);
	topDiv = document.getElementById("topDiv");
	var ev = ev||window.event;
	div = document.createElement("div");
	div.innerHTML = win.innerHTML;
	div.className = "dragedDiv";
	div.style.display="block";
	div.style.position="absolute";
	div.style.filter="alpha(opacity=70)";
	div.style.cursor="move";
	//div.style.border="1px solid #000000";
	div.style.width=win.style.width;
	div.style.height=win.style.height;
	div.style.top=winInfo.top;//parseInt(win.style.top)+3;
	div.style.left=winInfo.left;//parseInt(win.style.left)+3;
	win.parentNode.appendChild(div);
	var index = obj.style.zIndex + 1;
	div.style.zIndex = index;
	div.onmousemove = move;
	div.onmouseup = stopMove;
	x0 = ev.clientX;// + document.body.scrollLeft - document.body.clientLeft;
	y0 = ev.clientY;// + document.body.scrollTop - document.body.clientTop;
	x1 = parseInt(win.offsetLeft);
	y1 = parseInt(win.offsetTop);
	xx = x0 - x1;
	yy = y0 - y1;
	normal = win.style.border;
	win.style.border = "1px dotted #ff0000";
	moveable=true;
}

function stopMove(){
	moveable=false;
	var index = win.style.zIndex - 1;
	div.style.zIndex = 0;
	div.style.left = x1;
	div.style.top = y1;
	div.style.display = "none";
	topDiv.removeChild(div);
	if(win!=null){
		win.style.border = normal;
	}
}

function move(){
	var ev = ev||window.event;
	var ex = ev.clientX;
	var ey = ev.clientY;
	if(lastEx==0&&lastEy==0){
		lastEx = ex;
		lastEy = ey;
	}
	if(moveable){
		div.style.left = ex - xx;
		div.style.top  = ey - yy;
		for(var i=0;i<topDiv.childNodes.length;i++){
			var subDiv = topDiv.childNodes[i];
			var info = getInfo(subDiv);
			if(win!=subDiv&&div!=subDiv){
				document.all.left.value = info.left;
				document.all.right.value = info.right;
				document.all.top.value = info.top;
				document.all.bottom.value = info.bottom;
				document.all.x0.value = ex;
				document.all.y0.value = ey;
				if(ex>info.left && ex<info.right && ey>info.top && ey<info.bottom){
				if(lastEy>ey)
					topDiv.insertBefore(win,topDiv.childNodes[i]);
				else
					topDiv.insertBefore(win,topDiv.childNodes[i+1]);
				break;
				}
			}
		}
	}else{
		win.style.backgroundColor = "#006600";
	}
	lastEx = ex;
	lastEy = ey;
}
function mouseOut(){	
	stopMove();
}

function mouseOver(obj){
	obj.style.backgroundColor = "#cc0000";
}
function getInfo(o){
	var left=0;
	var right=0;
	var top=0;
	var bottom=0;
	var twidth=o.offsetWidth;
	var theight=o.offsetHeight;
	while(o!=topDiv){
		left += o.offsetLeft;
		top+=o.offsetTop;
		o=o.offsetParent;
	}
		right=left+twidth;
		bottom=top+theight;
	return {
		left:left,right:right,top:top,bottom:bottom
	};
}
window.onload = function(){
	;
};
function all(){
event.returnValue=false;
}
document.oncontextmenu=all;
document.onselectstart=all;